<?php
// app/Controller/UsersController.php
class UsersController extends AppController {

	public function beforeFilter() {
    	//parent::beforeFilter();
	}

	
	
    public function index() {
		$this->set('title_for_layout', 'Login');
		print "<pre>";
		print_r($this->request);
		print "</pre>";
		
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->redirect($this->Auth->redirect());
			} else {
				$this->Session->setFlash(__('Invalid username or password, try again'));
			}
		}
    }
	public function test(){
		$userinfo=$this->User->find('all');
		print "<pre>";
		print_r($userinfo);
		print "</pre>";
	}

    public function profile() {
		$this->loadModel('Profile');
		$this->set('title_for_layout', 'Profile');
		
		$userinfo=$this->User->find('all');
		
        $this->User->id=$userinfo[0]['User']['id'];
        /*if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }*/
        if ($this->request->is('post') || $this->request->is('put')) {
			$datas = $this->request->data;
			
			if(empty($datas['User']['password'])){
				array_splice($datas['User'],1);
			}
			
			
           if ($this->User->save($datas) && $this->Profile->save($datas)) {
                 $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'profile'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read();
			$data=$this->User->find('all');
			
			unset($this->request->data['User']['password']);
			$this->set('data',$data);
        }
    }
	
	function add(){
	
	}
	
	public function user(){
        if (!$this->User->exists()) {
           //throw new NotFoundException(__('Invalid user'));
        }
        $this->set('users', $this->User->find('all'));
	}

	


	public function login() {	
		$this->set('title_for_layout', 'Login');
	
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->redirect($this->Auth->redirect());
			} else {
				$this->Session->setFlash(__('Invalid username or password, try again'));
			}
		}
	}
	
	
	
	
	public function logout() {
		$this->Auth->logout();
		$this->redirect('/Users/login/');
	}
}